package com.yizhuoyan.his.service.pharmacyoperator.ServiceImpl;

import com.yizhuoyan.his.ao.DrugAo;
import com.yizhuoyan.his.dao.HosDrugsDao;
import com.yizhuoyan.his.dao.SysConstantDao;
import com.yizhuoyan.his.entity.HosDrugsEntity;
import com.yizhuoyan.his.entity.SysConstantEntity;
import com.yizhuoyan.his.service.pharmacyoperator.DrugManageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DrugManageServiceImpl implements DrugManageService {
    @Autowired
    HosDrugsDao dao;
    @Autowired
    SysConstantDao sDao;
    @Override
    public void addDrug(@Valid DrugAo ao) throws Exception {
      //清理参数
      //执行逻辑
        HosDrugsEntity d = new HosDrugsEntity();
        d.setCode(ao.getCode());
        d.setDrugDosageId(ao.getDrugDosageId());
        d.setDrugTypeId(ao.getDrugTypeId());
        d.setManufacturer(ao.getManufacturer());
        d.setMnemonic(ao.getMnemonic());
        d.setName(ao.getName());
        d.setSpecification(ao.getSpecification());
        d.setUnit(ao.getUnit());
        d.setUnitPrice(ao.getUnitPrice());
      //组装业务
        dao.insert(d);
        log.trace("插入一条数据");
    }

    @Override
    public void removeDrug( Long id) throws Exception {
        //清理参数
        //执行逻辑
        Map<String,Object> map = new HashMap<>();
        map.put("deleted",1);
        //组装业务
        dao.update(id,map);
        log.trace("修改完毕");
    }

    @Override
    public HosDrugsEntity queryDrug( Long id) throws Exception {
        //清理参数
        //执行逻辑
        HosDrugsEntity drugEntity = dao.selectById(id);
        //组装业务
        if(drugEntity==null){
            throw  new NullPointerException("药品查询为空");
        }
        log.trace("查询到药品");
        return drugEntity;

    }

    @Override
    public void modifyDrug( Long id, @NonNull DrugAo ao) throws Exception {
      //清理参数
      //执行逻辑
        Map<String,Object> map = new HashMap<>();
        map.put("code",ao.getCode());
        map.put("drugDosageId",ao.getDrugDosageId());
        map.put("drugTypeId",ao.getDrugTypeId());
        map.put("mnemonic",ao.getMnemonic());
        map.put("name",ao.getName());
        map.put("specification",ao.getSpecification());
        map.put("unitPrice",ao.getUnitPrice());
        map.put("manufacturer",ao.getManufacturer());
        map.put("unit",ao.getUnit());
      //组装业务
        dao.update(id,map);
        log.trace("修改了一条数据");
    }

    @Override
    public List<SysConstantEntity> queryDrugsType() throws Exception {
        List<SysConstantEntity> sysConstantEntities = sDao.selectDrugsType();
        if(sysConstantEntities==null){
            throw new NullPointerException("查询药品的类型为空");
        }
        return sysConstantEntities;
    }

    @Override
    public List<SysConstantEntity> queryDrugsDosage() throws Exception {
        List<SysConstantEntity> sysConstantEntities = sDao.selectDrugsDosage();
        if(sysConstantEntities==null){
            throw  new NullPointerException("查询药剂剂型为空");
        }
        return sysConstantEntities;
    }
}
